rm(list=ls(all=TRUE))
setwd() # input working directory
#install.packages("pacman")
pacman::p_load(dplyr, tidyr, tidyverse, infer, broom,
               descr, stargazer, gtools, boot, InteractionPoweR, pwr)

##### 1976 ######

# Read in dataframe
data76 <- read.csv("data76.csv")

# Rename variables
data76 <- data76 %>% rename(
  vn_tempboundaries = v112,
  early_vn = v113,
  late_vn = v114,
  vn_personal = v117,
  performance_containment = v118,
  performance_armscontrol = v121,
  performance_alliesdefense = v122,
  performance_security = v123,
  performance_energy = v125,
  performance_democracy = v126,
  performance_antiaggression = v129,
  performance_balanceofpower = v130,
  performance_un = v133,
  performance_strongallies = v134,
  performance_cooperation = v135,
  importance_containment = v136,
  importance_armscontrol = v139,
  importance_alliesdefense = v140,
  importance_security = v141,
  importance_energy = v143,
  importance_democracy = v144,
  importance_antiaggression = v147,
  importance_balanceofpower = v148,
  importance_un = v151,
  importance_strongallies = v152,
  importance_cooperation = v153,
  moral_israel = v154,
  us_isolationism = v156,
  ussr_relations = v158,
  containment_means = v160,
  vn_domino = v176,
  vn_mideast = v177,
  vn_usleadership = v178,
  vn_angola = v180,
  vn_chinaexpansionism = v206,
  vn_communistvictory = v208,
  vn_milaidwars = v215,
  vn_ussrexpansionism = v217,
  domino_theory = v218,
  mideast_intervention = v219,
  us_leadership = v220,
  angola_involvement = v222,
  chinese_expansionism = v223,
  un_disputes = v224,
  communist_victory = v225,
  communist_fragmentation = v230,
  milaid_wars = v232,
  ussr_expansionism = v234,
  vn_patience = v247,
  vn_alliances = v248,
  vn_airwars = v249,
  vn_nationalism = v250,
  vn_miladvice = v251,
  vn_uspower = v252,
  vn_limitedwar = v253,
  vn_detente = v254,
  vn_otherswars = v255,
  vn_appliedforce = v256,
  vn_pastmistakes = v257,
  vn_vitalinterests = v258,
  vn_weakallies = v259,
  vn_opponentspower = v260,
  vn_mildecline = v261,
  vn_power = v262,
  vn_milgoals = v263,
  usfp_patience = v264,
  usfp_alliances = v265,
  usfp_airwars = v266,
  usfp_nationalism = v267,
  usfp_miladvice = v268,
  usfp_airpower = v269,
  usfp_limitedwar = v270,
  usfp_detente = v271,
  usfp_otherswars = v272,
  usfp_appliedforce = v273,
  usfp_pastmistakes = v274,
  usfp_vitalinterests = v275,
  usfp_weakallies = v276,
  usfp_opponentspower = v277,
  usfp_mildecline = v278,
  usfp_power = v279,
  usfp_milgoals = v280,
  vn_credibility1 = v308,
  vn_commtriumphs = v309,
  vn_falsedetente = v310,
  vn_faithgovt = v311,
  vn_memoryinterests = v312,
  vn_systemalt = v314,
  vn_credibility2 = v315,
  vn_swingcommunism = v316,
  vn_limitedinterests = v317,
  vn_longtermthreats = v318,
  vn_domesticdissidents = v320,
  vn_unrealisticgoals = v321,
  vn_saigonpopular = v322,
  vn_massmedia = v323,
  vn_congress = v324,
  vn_sovietchina = v325,
  vn_watergate = v326,
  vn_nowin = v327,
  vn_thirdworldnat = v328,
  vn_restrictairpower = v329,
  vn_intelligence = v330,
  vn_lackgoals = v331,
  vn_attnmiladvice = v332,
  vn_lackallies = v333,
  vn_lackmilaid = v334,
  vn_73peace = v335,
  vn_worldopinion = v336,
  vn_nvndedication = v337,
  vn_relianceairpower = v338,
  vn_ignorance = v340,
  vn_munich = v342,
  vn_truman = v343,
  vn_marshall = v344,
  vn_berlin48 = v345,
  vn_korea = v346,
  vn_indochina = v347,
  vn_hungary = v348,
  vn_lebanon = v349,
  vn_bayofpigs = v350,
  vn_berlinwall = v351,
  vn_cubanmissiles = v352,
  gender = v353,
  age_categories = v354,
  education = v356,
  grad_edu = v357,
  #occupation = v358,
  mil_service = v374,
  ideology = v417,
  party_id = v425
)

###### Recode demographic and attitudinal variables ######

data76$wave <- rep(76, nrow(data76))

table(data76$party_id)
data76$party_id <- recode(data76$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)
summary(data76$party_id)

table(data76$age_categories)
data76$age_categories <- ifelse(data76$age_categories==0,NA,data76$age_categories)
summary(data76$age_categories)

table(data76$education)
data76$education <- ifelse(data76$education==8 | data76$education==9,NA,data76$education)
summary(data76$education)

table(data76$gender)
data76$gender <- ifelse(data76$gender==8 | data76$gender==9,NA,data76$gender)
data76$gender <- ifelse(data76$gender==2,1,0)
summary(data76$gender)

data76$elite_type <- ifelse(data76$id1<1999,"who's who",
                      ifelse(data76$id1>=2001 & data76$id1<=2499,"editorial writers",
                      ifelse(data76$id1>=2501 & data76$id1<=2999,"who's who, politics",
                      ifelse(data76$id1>=3001 & data76$id1<=3499,"press gallery",
                      ifelse(data76$id1>=3501 & data76$id1<=3999,"radio & tv",
                      ifelse(data76$id1>=4001 & data76$id1<=4499,"who's who, american women",
                      ifelse(data76$id1>=4501 & data76$id1<=4999,"protestant",
                      ifelse(data76$id1>=5001 & data76$id1<=5499,"foreign service",
                      ifelse(data76$id1>=5501 & data76$id1<=5999,"who's who, world jewery",
                      ifelse(data76$id1>=6001 & data76$id1<=6499,"official catholic dictionary",
                      ifelse(data76$id1>=6501 & data76$id1<=6999,"labor leaders",
                      ifelse(data76$id1>=7001 & data76$id1<=7499,"foreign policy elites",
                      ifelse(data76$id1>=7501 & data76$id1<7999,"IR specialists",
                      ifelse(data76$id1>=8001 & data76$id1<8499,"pentagon/DOD",
                      ifelse(data76$id1>=8501 & data76$id1<9400,"NPS monterrey","uncodable")))))))))))))))
table(data76$elite_type)

# Foreign service officers according to occupation
data76$fso1 <- ifelse(data76$occupation==81,1,0)
data76$fso2 <- ifelse(data76$elite_type=="foreign service",1,0)
crosstab(data76$fso1, data76$fso2, plot = F)
data76$fso <- ifelse(data76$fso1==1 | data76$fso2==1,1,0)
table(data76$fso)

# Military officers according to occupation
data76$mil_officer1 <- ifelse(data76$occupation==15 | data76$occupation==27 | data76$occupation==36 | data76$occupation==44 | data76$occupation==58,1,0)
data76$mil_officer2 <- ifelse(data76$elite_type=="pentagon/DOD" | data76$elite_type=="NPS monterrey",1,0)
data76$mil_officer <- ifelse(data76$mil_officer1==1 | data76$mil_officer2==1,1,0)
sum(data76$mil_officer1)

# Check coding and distribution of key variables
crosstab(data76$domino_theory, data76$fso, plot = F)
308/2282 # 0.1349693 matches "disagree strongly" in H&R 1984 Appendix B (coded as 0 in raw data)
413/2282 # 0.1809816 matches "disagree somewhat" (coded as 1 in raw data)
27/2282 # 0.01183173 matches "no opinion" (coded as 2 in raw data)
958/2292 # 0.4179756 matches "agree somewhat" (coded as 3 in raw data)
576/2282 # 0.2524102 matches "agree strongly" (coded as 4 in raw data)

data76$domino_theory <- ifelse(data76$domino_theory==0,1,
                        ifelse(data76$domino_theory==1,2,
                        ifelse(data76$domino_theory==3,3,
                        ifelse(data76$domino_theory==4,4,NA))))

crosstab(data76$angola_involvement, data76$fso, plot = F)
287/2282 # 0.1257669 matches "disagree strongly" in H&R Appendix B (coded as 0 in raw data)
546/2282 # 0.2392638 matches "disagree somewhat" (coded as 1 in raw data)
65/2282 # 0.02848379 matches "no opinion" (coded as 2 in raw data)
531/2282 # 0.2326906 matches "agree somewhat" (coded as 3 in raw data)
853/2282 # 0.3737949 matches "agree strongly" coded as 4 in raw data)

data76$angola_involvement <- ifelse(data76$angola_involvement==0,4,
                             ifelse(data76$angola_involvement==1,3,
                             ifelse(data76$angola_involvement==3,2,
                             ifelse(data76$angola_involvement==4,1,NA))))

data76$angola_involvement_bin <- ifelse(data76$angola_involvement>=3,1,0)

# Subset to just these observations
data76_dipmil <- data76[data76$mil_officer==1 | data76$fso == 1,]

      
##### 1980 ######

# Read in dataframe
data80 <- read.csv("data80.csv")

# Rename variables
data80 <- data80 %>% rename(
  early_vn = v514,
  late_vn = v515,
  early_iran = v517,
  late_iran = v518,
  vietnam_prestige = v519,
  iran_prestige = v520,
  afghan_prestige = v521,
  future_threats1 = v522,
  future_threats2 = v523,
  afghan_usinterests = v525,
  afghan_coldwar = v526,
  afghan_sovietsdefensive = v527,
  afghan_usdecline = v528,
  afghan_usoverreaction = v529,
  afghan_prochina = v530,
  afghan_grainembargo = v531,
  afghan_worstcrisis = v532,
  afghan_mideastbases = v533,
  afghan_detente = v534,
  afghan_persiangulf = v535,
  afghan_olympicboyott = v536,
  mobile_strike = v651,
  reduce_oildependence = v652,
  protect_shah = v645,
  soviets_defensive = v527,
  oil_embargo = v539,
  ussr_controlpersiangulf = v535,
  performance_containment = v713,
  performance_armscontrol = v715,
  performance_alliesdefense = v717,
  performance_security = v718,
  performance_energy = v719,
  performance_balanceofpower = v722,
  performance_humanrights = v723,
  performance_cooperation = v724,
  importance_containment = v667,
  importance_armscontrol = v670,
  importance_alliesdefense = v672,
  importance_security = v673,
  importance_energy = v675,
  importance_democracy = v676,
  importance_antiaggression = v680,
  importance_balanceofpower = v706,
  importance_humanrights = v707,
  importance_un = v709,
  importance_strongallies = v711,
  importance_cooperation = v712,
  moral_israel = v726,
  us_isolationism = v728,
  ussr_relations = v730,
  containment_means = v731,
  domino_theory = v538,
  mideast_intervention = v539,
  us_leadership = v540,
  chinese_expansionism = v542,
  un_disputes = v543,
  communist_victory = v544,
  communist_fragmentation = v547,
  milaid_wars = v549,
  ussr_expansionism = v551,
  third_world = v552,
  korea_commitment = v553,
  vn_causealliances = v555,
  vn_causeinvestments = v556,
  vn_causecontainchina = v557,
  vn_causedeternlwars = v558,
  vn_causebalanceofpower = v559,
  vn_causeforestallrightattack = v560,
  usfp_patience = v568,
  usfp_alliances = v569,
  usfp_airwars = v570,
  usfp_nationalism = v571,
  usfp_miladvice = v572,
  usfp_limitedwar = v573,
  usfp_detente = v574,
  usfp_otherswars = v575,
  usfp_appliedforce = v576,
  usfp_vitalinterests = v577,
  usfp_weakallies = v578,
  usfp_opponentspower = v579,
  usfp_mildecline = v580,
  usfp_power = v606,
  usfp_milgoals = v607,
  vn_effectspersist = v608,
  vn_credibility1 = v610,
  vn_commtriumphs = v611,
  vn_falsedetente = v612,
  vn_faithgovt = v613,
  vn_memoryinterests = v614,
  vn_systemalt = v615,
  vn_swingcommunism = v616,
  vn_limitedinterests = v617,
  vn_longtermthreats = v618,
  vn_lessonsgone = v619,
  vn_domesticdissidents = v622,
  vn_unrealisticgoals = v623,
  vn_saigonpopular = v624,
  vn_massmedia = v625,
  vn_congress = v626,
  vn_sovietchina = v627,
  vn_watergate = v628,
  vn_nowin = v629,
  vn_thirdworldnat = v630,
  vn_restrictairpower = v631,
  vn_intelligence = v632,
  vn_lackgoals = v633,
  vn_attnmiladvice = v634,
  vn_lackallies = v635,
  vn_lackmilaid = v636,
  vn_73peace = v637,
  vn_worldopinion = v638,
  vn_nvndedication = v639,
  vn_relianceairpower = v640,
  vn_unjust = v641,
  vn_ignorance = v642,
  #iran_protectshah = v645,
  iran_keepshahout = v646,
  iran_usdecline = v647,
  iran_avoidclosedregimes = v648,
  iran_ussrinvolved = v649,
  iran_returnshah = v650,
  iran_mobilestrikeforce = v651,
  iran_reduceoil = v652,
  iran_proairstrike = v653,
  iran_protectussr = v654,
  iran_deterfutureattacks = v655,
  iran_endvietnamsyndrome = v656,
  iran_sanctionsfutile = v657,
  iran_protectsaudis = v658,
  iran_patience = v659,
  iran_accomodate = v660,
  gender = v734,
  age_decade = v735,
  education = v737,
  grad_edu = v738,
  #occupation = v739,
  mil_service = v750,
  ideology = v765,
  party_id = v766
)

###### Recode demographic and attitudinal variables #####

data80$wave <- rep(80, nrow(data80))

table(data80$party_id)
data80$party_id <- recode(data80$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)

table(data80$age_decade)
data80$age_decade <- ifelse(data80$age_decade==9,NA,data80$age_decade)
data80$age_decade <- ifelse(data80$age_decade==0,6,
                     ifelse(data80$age_decade==1,5,
                     ifelse(data80$age_decade==2,4,
                     ifelse(data80$age_decade==3,3,
                     ifelse(data80$age_decade==4,2,
                     ifelse(data80$age_decade==5,1,
                     ifelse(data80$age_decade==6,0,NA)))))))

table(data80$education)
data80$education <- ifelse(data80$education==8 | data80$education==9,NA,data80$education)

table(data80$gender)
data80$gender <-  ifelse(data80$gender==8 | data80$gender==9,NA,data80$gender)
data80$gender <- ifelse(data80$gender==2,1,0)

data80$elite_type <- ifelse(data80$id1<1999,"who's who",
                     ifelse(data80$id1>=2001 & data80$id1<=2499,"press galleries",
                     ifelse(data80$id1>=2501 & data80$id1<=2999,"radio & tv galleries",
                     ifelse(data80$id1>=3001 & data80$id1<=3499,"editorial writers",
                     ifelse(data80$id1>=3501 & data80$id1<=3999,"foreign affairs experts",
                     ifelse(data80$id1>=4001 & data80$id1<=4499,"labor officials",
                     ifelse(data80$id1>=4501 & data80$id1<=4999,"who's who in american women",
                     ifelse(data80$id1>=5001 & data80$id1<=5499,"foreign service",
                     ifelse(data80$id1>=5501 & data80$id1<=5999,"who's who in american politics",
                     ifelse(data80$id1>=6001 & data80$id1<=6499,"catholics",
                     ifelse(data80$id1>=6501 & data80$id1<=6999,"protestants",
                     ifelse(data80$id1>=7001 & data80$id1<=7499,"pentagon officials",
                     ifelse(data80$id1>=7501 & data80$id1<7999,"jewry",                                                         
                     ifelse(data80$id1>=8001 & data80$id1<8499,"national war college",
                     ifelse(data80$id1>=8501 & data80$id1<9400,"1976 list","uncodable")))))))))))))))
table(data80$elite_type)

# Foreign service officers according to occupation
data80$fso1 <- ifelse(data80$occupation==16 | data80$occupation==28 | data80$occupation==37 | data80$occupation==45 | data80$occupation==64,1,0)
data80$fso2 <- ifelse(data80$elite_type=="foreign service",1,0)
crosstab(data80$fso1, data80$fso2, plot = F)
data80$fso <- ifelse(data80$fso1==1 | data80$fso2==1,1,0)
table(data80$fso)

# Military officers according to occupation
data80$mil_officer1 <- ifelse(data80$occupation==15 | data80$occupation==27 | data80$occupation==36 | data80$occupation==44 | data80$occupation==58,1,0)
data80$mil_officer2 <- ifelse(data80$elite_type=="national war college" | data80$elite_type=="pentagon officials",1,0)
data80$mil_officer <- ifelse(data80$mil_officer1==1 | data80$mil_officer2==1,1,0)
sum(data80$mil_officer)

# Check coding and distribution of key variables
crosstab(data80$domino_theory, data80$fso, plot = F)
392/2502 # 0.1566747 matches "disagree strongly" in H&R 1984 Appendix B (coded as 0 in raw data)
500/2502 # 0.1998401 matches "disagree somewhat" (coded as 1 in raw data)
57/2502 # 0.02278177 matches "no opinion" (coded as 2 in raw data)
995/2502 # 0.3976819 matches "agree somewhat" (coded as 3 in raw data)
558/2502 # 0.2524102 matches "agree strongly" (coded as 4 in raw data)

data80$domino_theory <- ifelse(data80$domino_theory==0,1,
                        ifelse(data80$domino_theory==1,2,
                        ifelse(data80$domino_theory==3,3,
                        ifelse(data80$domino_theory==4,4,NA))))

crosstab(data80$early_iran, data80$fso, plot = F) # this matches what is in the book
404/2502 # 0.1614708
736/2502 # 0.2941647
785/2502 # 0.313749
486/2502 # 0.1942446

data80$early_iran <- ifelse(data80$early_iran==1,4,
                     ifelse(data80$early_iran==2,1,
                     ifelse(data80$early_iran==3,2,
                     ifelse(data80$early_iran==4,3,NA))))

data80$early_iran_bin <- ifelse(data80$early_iran>=4,1,0)

crosstab(data80$late_iran, data80$fso, plot = F) # this roughly matches what is in the book
200/2502 # 0.07993605
692/2502 # 0.2765787
735/2502 # 0.293765
606/2502 # 0.2422062

data80$late_iran <- ifelse(data80$late_iran==1,4,
                    ifelse(data80$late_iran==2,1,
                    ifelse(data80$late_iran==3,2,
                    ifelse(data80$late_iran==4,3,NA))))

# Subset to just these observations
data80_dipmil <- data80[data80$mil_officer==1 | data80$fso == 1,]


##### 1984 ######

# Read in dataframe
data84 <- read.csv("data84.csv")

# Rename variables
data84 <- data84 %>% rename(
  assessment_lebanon = v112,
  early_vn = v116,
  assessment_elsalvador = v113,
  late_vn = v117,
  assessment_grenada = v114,
  assessment_nicaragua = v115,
  future_threats1 = v155,
  future_threats2 = v156,
  afghan_usinterests = v121,
  afghan_sovietsdefensive = v127,
  afghan_persiangulf = v132,
  early_lebanon = v118,
  late_lebanon = v119,
  afghan_aidrebels = v122,
  mobile_strike = v124,
  reduce_oildependence = v125,
  protect_shah = v126,
  soviets_defensive = v127,
  oil_embargo = v128,
  truck_bombing = v129,
  vietnam_syndrome = v131,
  ussr_controlpersiangulf = v132,
  performance_containment = v332,
  performance_armscontrol = v334,
  performance_alliesdefense = v335,
  performance_security = v336,
  performance_energy = v337,
  performance_democracy = v338,
  performance_matchsoviets = v341,
  performance_balanceofpower = v342,
  performance_humanrights = v344,
  performance_un = v346,
  performance_cooperation = v348,
  importance_containment = v314,
  importance_armscontrol = v316,
  importance_alliesdefense = v317,
  importance_security = v318,
  importance_energy = v319,
  importance_democracy = v320,
  importance_matchsoviets = v323,
  importance_balanceofpower = v324,
  importance_humanrights = v326,
  importance_un = v328,
  importance_cooperation = v330,
  moral_israel = v407,
  us_isolationism = v409,
  ussr_relations = v411,
  containment_means = v412,
  moral_taiwan = v413,
  domino_theory = v134,
  mideast_intervention = v128,
  us_leadership = v135,
  chinese_expansionism = v137,
  un_disputes = v138,
  communist_victory = v139,
  communist_fragmentation = v142,
  milaid_wars = v144,
  ussr_expansionism = v146,
  third_world = v147,
  korea_commitment = v230,
  ussr_fpgoals = v149,
  ussr_expansionrisks = v150,
  ussr_fear = v151,
  ussr_marx = v152,
  ussr_expansioninherent = v153,
  ussr_sharedinterests = v154,
  usfp_patience = v217,
  usfp_alliances = v218,
  usfp_nationalism = v219,
  usfp_miladvice = v220,
  usfp_limitedwar = v221,
  usfp_detente = v222,
  usfp_otherswars = v223,
  usfp_appliedforce = v224,
  usfp_vitalinterests = v225,
  usfp_opponentspower = v226,
  usfp_mildecline = v227,
  usfp_milgoals = v228,
  war_human = v232,
  war_aggression = v233,
  war_leaders = v234,
  war_nationalism = v235,
  war_anarchy = v236,
  war_ideology = v237,
  war_misperception = v238,
  war_powerpolitics = v239,
  war_econrivalries = v240,
  peace_milsuperiority = v241,
  peace_collectivesecurity = v242,
  peace_armscontrol = v243,
  peace_strengthenun = v245,
  peace_balanceofpower = v246,
  peace_coop = v247,
  vn_credibility1 = v160,
  vn_commtriumphs = v161,
  vn_falsedetente = v162,
  vn_faithgovt = v163,
  vn_memoryinterests = v164,
  vn_systemalt = v165,
  vn_swingcommunism = v166,
  vn_longtermthreats = v167,
  vn_lessonsgone = v168,
  vn_domesticdissidents = v250,
  vn_unrealisticgoals = v251,
  vn_saigonpopular = v252,
  vn_massmedia = v253,
  vn_congress = v254,
  vn_sovietchina = v255,
  vn_watergate = v256,
  vn_nowin = v257,
  vn_thirdworldnat = v258,
  vn_restrictairpower = v259,
  vn_lackgoals = v260,
  vn_attnmiladvice = v261,
  vn_lackmilaid = v262,
  vn_73peace = v263,
  vn_worldopinion = v264,
  vn_nvndedication = v265,
  vn_relianceairpower = v266,
  vn_ignorance = v267,
  iran_mobilestrikeforce = v124,
  euro_missiles = v273,
  advisers_elsalvador = v274,
  support_antisandinistas = v275,
  ussr_notrade = v276,
  troops_grenada = v277,
  supportgb_falklands = v278,
  ussr_koreaplane = v279,
  nuclear_freeze = v307,
  defense_spending = v308,
  sanction_poland = v309,
  nosanction_southafrica = v311,
  gender = v350,
  age_decade = v351,
  education = v353,
  grad_edu = v354,
  #occupation = v355,
  mil_service = v364,
  ideology = v369,
  party_id = v370
)

###### Recode demographic and attitudinal variables #####

data84$wave <- rep(84, nrow(data84))

table(data84$party_id)
data84$party_id <- recode(data84$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)

table(data84$age_decade)
data84$age_decade <- ifelse(data84$age_decade==9,NA,data84$age_decade)
data84$age_decade <- ifelse(data84$age_decade==0,6,
                     ifelse(data84$age_decade==1,5,
                     ifelse(data84$age_decade==2,4,
                     ifelse(data84$age_decade==3,3,
                     ifelse(data84$age_decade==4,2,
                     ifelse(data84$age_decade==5,1,
                     ifelse(data84$age_decade==6,0,NA)))))))

table(data84$education)
data84$education <- ifelse(data84$education==8 | data84$education==9,NA,data84$education)

table(data84$gender)
data84$gender <-  ifelse(data84$gender==8 | data84$gender==9,NA,data84$gender)
data84$gender <- ifelse(data84$gender==2,1,0)

data84$elite_type <- ifelse(data84$id1<41999,"who's who in america",
                     ifelse(data84$id1>=42001 & data84$id1<=42499,"foreign policy experts",
                     ifelse(data84$id1>=42501 & data84$id1<=42999,"press gallery",
                     ifelse(data84$id1>=43001 & data84$id1<=43499,"editorial writers",
                     ifelse(data84$id1>=43501 & data84$id1<=43999,"military & national war college",
                     ifelse(data84$id1>=44001 & data84$id1<=44499,"catholics",
                     ifelse(data84$id1>=44501 & data84$id1<=44999,"protestants",
                     ifelse(data84$id1>=45001 & data84$id1<=45499,"jewry",
                     ifelse(data84$id1>=45501 & data84$id1<=45999,"who's who in american politics",
                     ifelse(data84$id1>=46001 & data84$id1<=46499,"labor leaders",
                     ifelse(data84$id1>=46501 & data84$id1<=46999,"who's who in american women",
                     ifelse(data84$id1>=47001 & data84$id1<=47499,"foreign service",
                     ifelse(data84$id1>=47501 & data84$id1<47999,"prior respondents",
                     ifelse(data84$id1>=48001 & data84$id1<48499,"uncodable",NA))))))))))))))
table(data84$elite_type)

# Foreign service officers according to occupation
table(data84$occupation)
data84$fso1 <- ifelse(data84$occupation==19 | data84$occupation==31 | data84$occupation==40 | data84$occupation==48 | data84$occupation==55 | data84$occupation==61 | data84$occupation==70 | data84$occupation==73 | data84$occupation==76,1,0)
data84$fso2 <- ifelse(data84$elite_type=="foreign service",1,0)
crosstab(data84$fso1, data84$fso2)
data84$fso <- ifelse(data84$fso1==1 | data84$fso2==1,1,0)
table(data84$fso)

# Military officers according to occupation
data84$mil_officer1 <- ifelse(data84$occupation==15 | data84$occupation==27 | data84$occupation==36 | data84$occupation==44 | data84$occupation==51 | data84$occupation==58,1,0)
data84$mil_officer2 <- ifelse(data84$elite_type=="military & national war college",1,0)
data84$mil_officer <- ifelse(data84$mil_officer1==1 | data84$mil_officer2==1,1,0)
sum(data84$mil_officer)

# Recode key variables

data84$domino_theory <- ifelse(data84$domino_theory==1,4,
                        ifelse(data84$domino_theory==2,3,
                        ifelse(data84$domino_theory==3,2,
                        ifelse(data84$domino_theory==4,1,NA))))

data84$advisers_elsalvador <- ifelse(data84$advisers_elsalvador==1,4,
                                       ifelse(data84$advisers_elsalvador==2,3,
                                       ifelse(data84$advisers_elsalvador==3,2,
                                       ifelse(data84$advisers_elsalvador==4,1,NA))))

data84$advisers_elsalvador_bin <- ifelse(data84$advisers_elsalvador>=3,1,0)

data84$early_lebanon <- ifelse(data84$early_lebanon==1,3,
                        ifelse(data84$early_lebanon==2,1,
                        ifelse(data84$early_lebanon==3,2,NA)))

data84$early_lebanon_bin <- ifelse(data84$early_lebanon>=3,1,0)

data84$late_lebanon <- ifelse(data84$late_lebanon==1,3,
                        ifelse(data84$late_lebanon==2,1,
                        ifelse(data84$late_lebanon==3,2,NA)))

data84$troops_grenada <- ifelse(data84$troops_grenada==1,4,
                                ifelse(data84$troops_grenada==2,3,
                                ifelse(data84$troops_grenada==3,2,
                                ifelse(data84$troops_grenada==4,1,NA))))

data84$troops_grenada_bin <- ifelse(data84$troops_grenada>=3,1,0)

# Subset to just these observations
data84_dipmil <- data84[data84$mil_officer==1 | data84$fso == 1,]


##### 1988 ######

# Read in dataframe
data88 <- read.csv("data88.csv")

# Rename variables
data88 <- data88 %>% rename(
  assessment_gulfwar = v112,
  early_vn = v117,
  assessment_philippines = v113,
  late_vn = v118,
  assessment_nicaragua = v114,
  assessment_afghanistan = v115,
  future_threats1 = v127,
  future_threats2 = v128,
  reduce_oildependence = v142,
  oil_embargo = v250,
  ussr_controlpersiangulf = v153,
  performance_containment = v341,
  performance_armscontrol = v343,
  performance_alliesdefense = v344,
  performance_energy = v345,
  performance_democracy = v346,
  performance_matchsoviets = v348,
  performance_humanrights = v350,
  performance_un = v351,
  performance_cooperation = v354,
  importance_containment = v324,
  importance_armscontrol = v326,
  importance_alliesdefense = v327,
  importance_security = v328,
  importance_democracy = v329,
  importance_matchsoviets = v331,
  importance_humanrights = v333,
  importance_un = v335,
  importance_cooperation = v337,
  moral_israel = v316,
  us_isolationism = v318,
  ussr_relations = v320,
  containment_means = v321,
  moral_taiwan = v322,
  domino_theory = v129,
  mideast_intervention = v250,
  us_leadership = v130,
  chinese_expansionism = v133,
  un_disputes = v132,
  communist_victory = v134,
  communist_fragmentation = v137,
  milaid_wars = v139,
  ussr_expansionism = v140,
  third_world = v141,
  korea_commitment = v249,
  ussr_fpgoals = v147,
  ussr_expansionrisks = v148,
  ussr_fear = v149,
  ussr_marx = v146,
  ussr_expansioninherent = v150,
  ussr_sharedinterests = v151,
  us_expansionism = v143,
  us_leadershipdecline = v144,
  ussr_gorbachevgoals = v152,
  ussr_internalchanges = v251,
  us_withdraweurope = v252,
  usfp_nationalism = v238,
  usfp_miladvice = v239,
  usfp_limitedwar = v240,
  usfp_detente = v241,
  usfp_otherswars = v242,
  usfp_appliedforce = v243,
  usfp_vitalinterests = v244,
  usfp_opponentspower = v245,
  usfp_mildecline = v246,
  usfp_milgoals = v247,
  war_human = v220,
  war_aggression = v221,
  war_leaders = v222,
  war_nationalism = v223,
  war_anarchy = v224,
  war_ideology = v225,
  war_misperception = v226,
  war_powerpolitics = v227,
  war_econrivalries = v228,
  peace_milsuperiority = v229,
  peace_collectivesecurity = v230,
  peace_armscontrol = v232,
  peace_strengthenun = v234,
  peace_balanceofpower = v235,
  peace_coop = v231,
  advisers_elsalvador = v266,
  support_antisandinistas = v267,
  ussr_notrade = v268,
  defense_spending = v270,
  nosanction_southafrica = v272,
  arms_iran = v265,
  inf_agreement = v271,
  support_contras = v273,
  replace_marcos = v274,
  warpowers_persiangulf = v275,
  exclude_sdi = v276,
  aid_angola = v277,
  bomb_libya = v279,
  arms_saudis = v280,
  gender = v358,
  age_decade = v360,
  education = v361,
  grad_edu = v362,
  #occupation = v363,
  mil_service = v374,
  ideology = v376,
  party_id = v377
)

###### Recode demographic and attitudinal variables #####

data88$wave <- rep(88, nrow(data88))

table(data88$party_id)
data88$party_id <- recode(data88$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)

table(data88$age_decade)
data88$age_decade <- ifelse(data88$age_decade==9,NA,data88$age_decade)
data88$age_decade <- ifelse(data88$age_decade==0,6,
                     ifelse(data88$age_decade==1,5,
                     ifelse(data88$age_decade==2,4,
                     ifelse(data88$age_decade==3,3,
                     ifelse(data88$age_decade==4,2,
                     ifelse(data88$age_decade==5,1,
                     ifelse(data88$age_decade==6,0,NA)))))))

table(data88$education)
data88$education <- ifelse(data88$education==8 | data88$education==9,NA,data88$education)

table(data88$gender)
data88$gender <-  ifelse(data88$gender==8 | data88$gender==9,NA,data88$gender)
data88$gender <- ifelse(data88$gender==2,1,0)

data88$elite_type <- ifelse(data88$id1<51999,"who's who in america",
                     ifelse(data88$id1>=52001 & data88$id1<=52499,"foreign policy experts",
                     ifelse(data88$id1>=52501 & data88$id1<=52999,"media",
                     ifelse(data88$id1>=53001 & data88$id1<=53499,"defense",
                     ifelse(data88$id1>=53501 & data88$id1<=53999,"national war college",
                     ifelse(data88$id1>=54001 & data88$id1<=54499,"clergy",
                     ifelse(data88$id1>=54501 & data88$id1<=54999,"politicians",
                     ifelse(data88$id1>=55001 & data88$id1<=55499,"labor",
                     ifelse(data88$id1>=55501 & data88$id1<=55999,"women",
                     ifelse(data88$id1>=56001 & data88$id1<=56499,"state department",
                     ifelse(data88$id1>=56501 & data88$id1<=56999,"prior respondents",
                     ifelse(data88$id1>=57001,"uncodable",NA))))))))))))
table(data88$elite_type)

# Foreign service officers according to occupation
table(data88$occupation)
data88$fso1 <- ifelse(data88$occupation==19 | data88$occupation==31 | data88$occupation==40 | data88$occupation==48 | data88$occupation==55 | data88$occupation==61 | data88$occupation==70 | data88$occupation==73 | data88$occupation==76,1,0)
data88$fso2 <- ifelse(data88$elite_type=="state department",1,0)
crosstab(data88$fso1, data88$fso2)
data88$fso <- ifelse(data88$fso1==1 | data88$fso2==1,1,0)
table(data88$fso)

# Military officers according to occupation
data88$mil_officer1 <- ifelse(data88$occupation==15 | data88$occupation==27 | data88$occupation==36 | data88$occupation==44 | data88$occupation==51 | data88$occupation==58,1,0)
data88$mil_officer2 <- ifelse(data88$elite_type=="defense" | data88$elite_type=="national war college",1,0)
data88$mil_officer <- ifelse(data88$mil_officer1==1 | data88$mil_officer2==1,1,0)
sum(data88$mil_officer)

# Recode key variables

crosstab(data88$domino_theory, data88$fso, plot = F)

data88$domino_theory <- ifelse(data88$domino_theory==1,4,
                        ifelse(data88$domino_theory==2,3,
                        ifelse(data88$domino_theory==3,2,
                        ifelse(data88$domino_theory==4,1,NA))))

data88$advisers_elsalvador <- ifelse(data88$advisers_elsalvador==1,4,
                              ifelse(data88$advisers_elsalvador==2,3,
                              ifelse(data88$advisers_elsalvador==3,2,
                              ifelse(data88$advisers_elsalvador==4,1,NA))))

data88$advisers_elsalvador_bin <- ifelse(data88$advisers_elsalvador>=3,1,0)

data88$bomb_libya <- ifelse(data88$bomb_libya==1,4,
                     ifelse(data88$bomb_libya==2,3,
                     ifelse(data88$bomb_libya==3,2,
                     ifelse(data88$bomb_libya==4,1,NA))))

data88$bomb_libya_bin <- ifelse(data88$bomb_libya>=3,1,0)

# Subset to just these observations
data88_dipmil <- data88[data88$mil_officer==1 | data88$fso == 1,]


##### 1992 ######

# Read in dataframe
data92 <- read.csv("data92.csv")

# Rename variables
data92 <- data92 %>% rename(
  early_gulfwar = v112,
  early_vn = v234,
  late_gulfwar = v113,
  late_vn = v235,
  reduce_oildependence = v314,
  invade_panama = v125,
  protect_aquino = v129,
  states_traditionalinterests = v142,
  efficacy_force = v143,
  world_govt = v144,
  collective_security = v145,
  nationalism = v147,
  un_influence = v148,
  future_fragmentation = v149,
  us_dominant = v150,
  nonstrategy_military = v151,
  neglect_thirdworld = v152,
  system_polarity = v153,
  gulfwar_vn = v155,
  gulfwar_spending = v156,
  gulfwar_victory = v157,
  gulfwar_iraqis = v158,
  gulfwar_influence = v159,
  gulfwar_hatred = v160,
  gulfwar_leverage = v161,
  gulfwar_force = v162,
  allies_defendselves = v174,
  mil_strength = v175,
  mil_reliance = v176,
  new_coalitions = v177,
  troops_abroad = v208,
  intervene_order = v209,
  us_notsuperpower = v210,
  performance_containment = v334,
  performance_armscontrol = v336,
  performance_alliesdefense = v337,
  performance_energy = v338,
  performance_democracy = v339,
  performance_matchsoviets = v340,
  performance_antiaggression = v342,
  performance_humanrights = v341,
  performance_un = v344,
  performance_cooperation = v346,
  importance_containment = v316,
  importance_armscontrol = v318,
  importance_alliesdefense = v319,
  importance_energy = v320,
  importance_democracy = v321,
  importance_antiaggression = v324,
  importance_matchsoviets = v322,
  importance_humanrights = v323,
  importance_un = v326,
  importance_cooperation = v328,
  moral_israel = v211,
  us_isolationism = v213,
  ussr_relations = v215,
  containment_means = v216,
  moral_taiwan = v217,
  interventions_notourbusiness = v224,
  domino_theory = v275,
  us_leadership = v276,
  chinese_expansionism = v279,
  un_disputes = v278,
  communist_victory = v280,
  communist_fragmentation = v309,
  milaid_wars = v311,
  ussr_expansionism = v312,
  third_world = v313,
  korea_commitment = v362,
  ussr_internalchanges = v363,
  us_withdraweurope = v365,
  us_mideastpresence = v364,
  usfp_nationalism = v353,
  usfp_miladvice = v354,
  usfp_limitedwar = v355,
  usfp_otherswars = v356,
  usfp_appliedforce = v357,
  usfp_opponentspower = v358,
  usfp_mildecline = v360,
  usfp_milgoals = v361,
  war_human = v237,
  war_aggression = v238,
  war_leaders = v239,
  war_nationalism = v240,
  war_anarchy = v241,
  war_ideology = v242,
  war_misperception = v243,
  war_powerpolitics = v244,
  war_econrivalries = v245,
  peace_milsuperiority = v246,
  peace_collectivesecurity = v247,
  peace_armscontrol = v249,
  peace_strengthenun = v251,
  peace_balanceofpower = v252,
  peace_coop = v248,
  arms_saudis = v135,
  gender = v407,
  age_decade = v409,
  education = v410,
  grad_edu = v411,
  #occupation = v412,
  mil_service = v421,
  ideology = v425,
  party_id = v426
)

###### Recode demographic and attitudinal variables #####

data92$wave <- rep(92, nrow(data92))

table(data92$party_id)
data92$party_id <- recode(data92$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)

table(data92$age_decade)
data92$age_decade <- ifelse(data92$age_decade==9,NA,data92$age_decade)
data92$age_decade <- ifelse(data92$age_decade==0,6,
                     ifelse(data92$age_decade==1,5,
                     ifelse(data92$age_decade==2,4,
                     ifelse(data92$age_decade==3,3,
                     ifelse(data92$age_decade==4,2,
                     ifelse(data92$age_decade==5,1,
                     ifelse(data92$age_decade==6,0,NA)))))))

table(data92$education)
data92$education <- ifelse(data92$education==8 | data92$education==9,NA,data92$education)

table(data92$gender)
data92$gender <-  ifelse(data92$gender==8 | data92$gender==9,NA,data92$gender)
data92$gender <- ifelse(data92$gender==2,1,0)

data92$elite_type <- ifelse(data92$id1<61999,"who's who in america",
                     ifelse(data92$id1>=62001 & data92$id1<=62499,"media",
                     ifelse(data92$id1>=62501 & data92$id1<=62999,"politicians",
                     ifelse(data92$id1>=63001 & data92$id1<=63499,"experts",
                     ifelse(data92$id1>=63501 & data92$id1<=63999,"labor",
                     ifelse(data92$id1>=64001 & data92$id1<=64499,"clergy",
                     ifelse(data92$id1>=64501 & data92$id1<=64999,"national war college",
                     ifelse(data92$id1>=65001 & data92$id1<=65499,"state department",
                     ifelse(data92$id1>=65501 & data92$id1<=65999,"women",
                     ifelse(data92$id1>=66001 & data92$id1<=66499,"prior respondents",
                     ifelse(data92$id1>=66501 & data92$id1<=66999,"defense",
                     ifelse(data92$id1>=67001,"uncodable",NA))))))))))))
table(data92$elite_type)

# Foreign service officers according to occupation
table(data92$occupation)
data92$fso1 <- ifelse(data92$occupation==19 | data92$occupation==31 | data92$occupation==40 | data92$occupation==48 | data92$occupation==55 | data92$occupation==61 | data92$occupation==70 | data92$occupation==73 | data92$occupation==76,1,0)
data92$fso2 <- ifelse(data92$elite_type=="state department",1,0)
crosstab(data92$fso1, data92$fso2)
data92$fso <- ifelse(data92$fso1==1 | data92$fso2==1,1,0)
table(data92$fso)

# Military officers according to occupation
data92$mil_officer1 <- ifelse(data92$occupation==15 | data92$occupation==27 | data92$occupation==36 | data92$occupation==44 | data92$occupation==51 | data92$occupation==58,1,0)
data92$mil_officer2 <- ifelse(data92$elite_type=="national war college" | data92$elite_type=="defense",1,0)
data92$mil_officer <- ifelse(data92$mil_officer1==1 | data92$mil_officer2==1,1,0)
sum(data92$mil_officer)

# Recode key variables

data92$domino_theory <- ifelse(data92$domino_theory==1,4,
                        ifelse(data92$domino_theory==2,3,
                        ifelse(data92$domino_theory==3,2,
                        ifelse(data92$domino_theory==4,1,NA))))

data92$invade_panama <- ifelse(data92$invade_panama==1,4,
                               ifelse(data92$invade_panama==2,3,
                               ifelse(data92$invade_panama==3,2,
                               ifelse(data92$invade_panama==4,1,NA))))

data92$invade_panama_bin <- ifelse(data92$invade_panama>=3,1,0)

data92$early_gulfwar <- ifelse(data92$early_gulfwar==1,3,
                               ifelse(data92$early_gulfwar==2,2,
                               ifelse(data92$early_gulfwar==3,1,NA)))

data92$early_gulfwar_bin <- ifelse(data92$early_gulfwar>=3,1,0)

data92$late_gulfwar <- ifelse(data92$late_gulfwar==1,3,
                               ifelse(data92$late_gulfwar==2,2,
                               ifelse(data92$late_gulfwar==3,1,NA)))

# Subset to just these observations
data92_dipmil <- data92[data92$mil_officer==1 | data92$fso == 1,]


##### 1996 ######

# Read in dataframe
data96 <- read.csv("data96.csv")

# Rename variables
data96 <- data96 %>% rename(
  early_bosnia = v112,
  early_vn = v322,
  late_bosnia = v113,
  late_vn = v323,
  states_traditionalinterests = v219,
  efficacy_force = v220,
  world_govt = v221,
  collective_security = v222,
  nationalism = v224,
  future_fragmentation = v225,
  us_dominant = v226,
  nonstrategy_military = v227,
  neglect_thirdworld = v228,
  system_polarity = v231,
  gulfwar_vn = v169,
  gulfwar_spending = v170,
  gulfwar_victory = v171,
  gulfwar_influence = v172,
  gulfwar_saddam1 = v173,
  allies_defendselves = v255,
  mil_strength = v256,
  new_coalitions = v257,
  troops_abroad = v258,
  intervene_order = v260,
  force_russia1 = v114,
  force_iraq = v115,
  force_cuba = v116,
  force_israel = v117,
  force_korea = v118,
  force_russia2 = v119,
  force_southafrica = v120,
  force_rwanda = v121,
  force_taiwan = v122,
  force_panama = v123,
  force_haiti = v124,
  force_mexico = v125,
  force_un = v127,
  force_nato = v128,
  force_multilateral = v129,
  force_congress = v130,
  force_president = v131,
  us_superpower = v161,
  troops_aidsomalia = v175,
  troops_endsomaliawar = v176,
  troops_haiti = v179,
  performance_containment = v364,
  performance_armscontrol = v366,
  performance_alliesdefense = v367,
  performance_energy = v368,
  performance_democracy = v369,
  performance_antiaggression = v371,
  performance_humanrights = v370,
  performance_un = v373,
  performance_strongallies = v375,
  importance_containment = v343,
  importance_armscontrol = v344,
  importance_alliesdefense = v346,
  importance_energy = v347,
  importance_democracy = v348,
  importance_antiaggression = v350,
  importance_humanrights = v349,
  importance_un = v352,
  importance_cooperation = v354,
  importance_milsuperiority = v360,
  moral_israel = v264,
  us_isolationism = v266,
  ussr_relations = v268,
  containment_means = v269,
  moral_taiwan = v270,
  interventions_notourbusiness = v310,
  us_commandun = v361,
  us_commandnato = v362,
  domino_theory = v325,
  us_leadership = v261,
  chinese_expansionism = v327,
  un_disputes = v326,
  communist_victory = v328,
  milaid_wars = v331,
  ussr_expansionism = v332,
  third_world = v333,
  us_mideastpresence = v167,
  usfp_otherswars = v337,
  usfp_vitalinterests = v259,
  usfp_opponentspower = v338,
  usfp_milgoals = v339,
  war_human = v272,
  war_aggression = v273,
  war_leaders = v274,
  war_nationalism = v275,
  war_anarchy = v276,
  war_ideology = v277,
  war_misperception = v278,
  war_econrivalries = v279,
  peace_milsuperiority = v132,
  peace_collectivesecurity = v133,
  peace_armscontrol = v135,
  peace_strengthenun = v137,
  peace_balanceofpower = v138,
  peace_coop = v134,
  gender = v410,
  age_decade = v412,
  education = v413,
  grad_edu = v414,
  #occupation = v415,
  mil_service = v425,
  ideology = v427,
  party_id = v428
)

###### Recode demographic and attitudinal variables #####

data96$wave <- rep(96, nrow(data96))

table(data96$party_id)
data96$party_id <- recode(data96$party_id, `1` = 3, `2` = 1, `3` = 2, .default = NA_real_)

table(data96$age_decade)
data96$age_decade <- ifelse(data96$age_decade==9,NA,data96$age_decade)
data96$age_decade <- ifelse(data96$age_decade==0,6,
                     ifelse(data96$age_decade==1,5,
                     ifelse(data96$age_decade==2,4,
                     ifelse(data96$age_decade==3,3,
                     ifelse(data96$age_decade==4,2,
                     ifelse(data96$age_decade==5,1,
                     ifelse(data96$age_decade==6,0,NA)))))))

table(data96$education)
data96$education <- ifelse(data96$education==8 | data96$education==9,NA,data96$education)

table(data96$gender)
data96$gender <-  ifelse(data96$gender==8 | data96$gender==9,NA,data96$gender)
data96$gender <- ifelse(data96$gender==2,1,0)

data96$elite_type <- ifelse(data96$id1<71999,"who's who in america",
                     ifelse(data96$id1>=72001 & data96$id1<=72499,"media",
                     ifelse(data96$id1>=72501 & data96$id1<=72999,"politicians",
                     ifelse(data96$id1>=73001 & data96$id1<=73499,"experts",
                     ifelse(data96$id1>=73501 & data96$id1<=73999,"labor",
                     ifelse(data96$id1>=74001 & data96$id1<=74499,"clergy",
                     ifelse(data96$id1>=74501 & data96$id1<=74999,"national war college",
                     ifelse(data96$id1>=75001 & data96$id1<=75499,"state department",
                     ifelse(data96$id1>=75501 & data96$id1<=75999,"women",
                     ifelse(data96$id1>=76001 & data96$id1<=76499,"prior respondents",
                     ifelse(data96$id1>=76501 & data96$id1<=76999,"military",
                     ifelse(data96$id1>=77001,"uncodable",NA))))))))))))
table(data96$elite_type)

# Foreign service officers according to occupation
table(data96$occupation)
data96$fso1 <- ifelse(data96$occupation==19 | data96$occupation==31 | data96$occupation==40 | data96$occupation==48 | data96$occupation==55 | data96$occupation==61 | data96$occupation==70 | data96$occupation==73 | data96$occupation==76,1,0)
data96$fso2 <- ifelse(data96$elite_type=="state department",1,0)
crosstab(data96$fso1, data96$fso2)
data96$fso <- ifelse(data96$fso1==1 | data96$fso2==1,1,0)
table(data96$fso)

# Military officers according to occupation
data96$mil_officer1 <- ifelse(data96$occupation==15 | data96$occupation==27 | data96$occupation==36 | data96$occupation==44 | data96$occupation==51 | data96$occupation==58,1,0)
data96$mil_officer2 <- ifelse(data96$elite_type=="national war college" | data96$elite_type=="military",1,0)
crosstab(data96$mil_officer1, data96$mil_officer2)
data96$mil_officer <- ifelse(data96$mil_officer1==1 | data96$mil_officer2==1,1,0)
sum(data96$mil_officer)

# Military service
table(data96$mil_service)

# Recode key variables

data96$domino_theory <- ifelse(data96$domino_theory==1,4,
                        ifelse(data96$domino_theory==2,3,
                        ifelse(data96$domino_theory==3,2,
                        ifelse(data96$domino_theory==4,1,NA))))

data96$early_bosnia <- ifelse(data96$early_bosnia==1,1,
                             ifelse(data96$early_bosnia==2,0,NA))

data96$late_bosnia <- ifelse(data96$late_bosnia==1,1,
                             ifelse(data96$late_bosnia==2,0,NA))

data96$force_russia1 <- ifelse(data96$force_russia1==1,1,
                               ifelse(data96$force_russia1==2,0,NA))

data96$force_iraq <- ifelse(data96$force_iraq==1,1,
                            ifelse(data96$force_iraq==2,0,NA))

data96$force_cuba <- ifelse(data96$force_cuba==1,1,
                            ifelse(data96$force_cuba==2,0,NA))

data96$force_israel <- ifelse(data96$force_israel==1,1,
                              ifelse(data96$force_israel==2,0,NA))

data96$force_korea <- ifelse(data96$force_korea==1,1,
                             ifelse(data96$force_korea==2,0,NA))

data96$force_russia2 <- ifelse(data96$force_russia2==1,1,
                               ifelse(data96$force_russia2==2,0,NA))

data96$force_rwanda <- ifelse(data96$force_rwanda==1,1,
                              ifelse(data96$force_rwanda==2,0,NA))

data96$force_taiwan <- ifelse(data96$force_taiwan==1,1,
                              ifelse(data96$force_taiwan==2,0,NA))

data96$force_panama <- ifelse(data96$force_panama==1,1,
                              ifelse(data96$force_panama==2,0,NA))

data96$force_haiti <- ifelse(data96$force_haiti==1,1,
                             ifelse(data96$force_haiti==2,0,NA))

data96$troops_haiti <- ifelse(data96$troops_haiti==1,1,
                              ifelse(data96$troops_haiti==2,0,NA))

data96$force_mexico <- ifelse(data96$force_mexico==1,1,
                              ifelse(data96$force_mexico==2,0,NA))

# Subset to just these observations
data96_dipmil <- data96[data96$mil_officer==1 | data96$fso == 1,]


##### Pooled data #####

###### Subset and recode key variables ######

data <- smartbind(data76_dipmil, data80_dipmil, data84_dipmil, data88_dipmil, data92_dipmil, data96_dipmil)
table(data$fso)
table(data$angola_involvement)
table(data$early_iran_bin)
table(data$advisers_elsalvador)
table(data$early_lebanon)
table(data$troops_grenada)

data$age <- coalesce(data$age_categories, data$age_decade)

data <- data %>% mutate(use_force = case_when(angola_involvement >=3 | early_iran>=4 | advisers_elsalvador>=3 | early_lebanon>=3 | troops_grenada>=3 | bomb_libya>=3 | invade_panama>=3 | early_gulfwar>=3 | early_bosnia==1 | force_russia1==1 | force_iraq==1 | force_cuba==1 | force_israel==1 | force_korea==1 | force_russia2==1 | force_taiwan==1~1, TRUE ~ 0))
table(data$use_force)

data$angola_dum <- ifelse(!is.na(data$angola_involvement),1,0)
data$iran_dum <- ifelse(!is.na(data$early_iran),1,0)
table(data$early_iran)
table(data$iran_dum)
data$elsalvador_dum <- ifelse(!is.na(data$advisers_elsalvador),1,0)
table(data$advisers_elsalvador)
table(data$elsalvador_dum)
data$lebanon_dum <- ifelse(!is.na(data$early_lebanon),1,0)
table(data$early_lebanon)
table(data$lebanon_dum)
data$grenada_dum <- ifelse(!is.na(data$troops_grenada),1,0)
table(data$grenada_dum)
data$libya_dum <- ifelse(!is.na(data$bomb_libya),1,0)
table(data$libya_dum)
data$panama_dum <- ifelse(!is.na(data$invade_panama),1,0)
data$gulfwar_dum <- ifelse(!is.na(data$early_gulfwar),1,0)
data$bosnia_dum <- ifelse(!is.na(data$early_bosnia),1,0)
data$russia1_dum <- ifelse(!is.na(data$force_russia1),1,0)
data$iraq_dum <- ifelse(!is.na(data$force_iraq),1,0)
data$cuba_dum <- ifelse(!is.na(data$force_cuba),1,0)
data$israel_dum <- ifelse(!is.na(data$force_israel),1,0)
data$korea_dum <- ifelse(!is.na(data$force_korea),1,0)
data$russia2_dum <- ifelse(!is.na(data$force_russia2),1,0)
data$taiwan_dum <- ifelse(!is.na(data$force_taiwan),1,0)

data$domino_theory_bin <- ifelse(data$domino_theory>=3,1,0)

###### Descriptive Statistics (Tables A17-A28) #####

#1976
dip_desc76 <- data[data$fso==1 & data$wave==76,]
dip_desc76 <- data.frame(dip_desc76$age_categories,dip_desc76$gender,dip_desc76$education,dip_desc76$party_id,dip_desc76$domino_theory_bin,dip_desc76$angola_involvement)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","Angola Involvement")

# Table A17
stargazer(dip_desc76,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          omit = "data.MilExp",
          covariate.labels = covs_dip)

mil_desc76 <- data[data$mil_officer==1 & data$wave==76,]
mil_desc76 <- data.frame(mil_desc76$age_categories,mil_desc76$gender,mil_desc76$education,mil_desc76$party_id,mil_desc76$domino_theory_bin,mil_desc76$angola_involvement)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","Angola Involvement")

# Table A18
stargazer(mil_desc76,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 

#1980
dip_desc80 <- data[data$fso==1 & data$wave==80,]
dip_desc80 <- data.frame(dip_desc80$age_decade,dip_desc80$gender,dip_desc80$education,dip_desc80$party_id,dip_desc80$domino_theory_bin,dip_desc80$early_iran)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","Early Iran")

# Table A19
stargazer(dip_desc80,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_dip)

mil_desc80 <- data[data$mil_officer==1 & data$wave==80,]
mil_desc80 <- data.frame(mil_desc80$age_decade,mil_desc80$gender,mil_desc80$education,mil_desc80$party_id,mil_desc80$domino_theory_bin,mil_desc80$early_iran)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","Early Iran")

# Table A20
stargazer(mil_desc80,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 

#1984
dip_desc84 <- data[data$fso==1 & data$wave==84,]
dip_desc84 <- data.frame(dip_desc84$age_decade,dip_desc84$gender,dip_desc84$education,dip_desc84$party_id,dip_desc84$domino_theory_bin,dip_desc84$advisers_elsalvador,dip_desc84$early_lebanon,dip_desc84$troops_grenada)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","El Salvador","Early Lebanon","Grenada")

# Table A21
stargazer(dip_desc84,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_dip)

mil_desc84 <- data[data$mil_officer==1 & data$wave==84,]
mil_desc84 <- data.frame(mil_desc84$age_decade,mil_desc84$gender,mil_desc84$education,mil_desc84$party_id,mil_desc84$domino_theory_bin,mil_desc84$advisers_elsalvador,mil_desc84$early_lebanon,mil_desc84$troops_grenada)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","El Salvador","Early Lebanon","Grenada")

# Table A22
stargazer(mil_desc84,
          type = "latex",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 

#1988
dip_desc88 <- data[data$fso==1 & data$wave==88,]
dip_desc88 <- data.frame(dip_desc88$age_decade,dip_desc88$gender,dip_desc88$education,dip_desc88$party_id,dip_desc88$domino_theory_bin,dip_desc88$advisers_elsalvador,dip_desc88$bomb_libya)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","El Salvador","Libya")

# Table A23
stargazer(dip_desc88,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_dip)

mil_desc88 <- data[data$mil_officer==1 & data$wave==88,]
mil_desc88 <- data.frame(mil_desc88$age_decade,mil_desc88$gender,mil_desc88$education,mil_desc88$party_id,mil_desc88$domino_theory_bin,mil_desc88$advisers_elsalvador,mil_desc88$bomb_libya)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","El Salvador","Libya")

# Table A24
stargazer(mil_desc88,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 

#1992
dip_desc92 <- data[data$fso==1 & data$wave==92,]
dip_desc92 <- data.frame(dip_desc92$age_decade,dip_desc92$gender,dip_desc92$education,dip_desc92$party_id,dip_desc92$domino_theory_bin,dip_desc92$invade_panama,dip_desc92$early_gulfwar)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","Panama","Gulf War")

# Table A25
stargazer(dip_desc92,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_dip)

mil_desc92 <- data[data$mil_officer==1 & data$wave==92,]
mil_desc92 <- data.frame(mil_desc92$age_decade,mil_desc92$gender,mil_desc92$education,mil_desc92$party_id,mil_desc92$domino_theory_bin,mil_desc92$invade_panama,mil_desc92$early_gulfwar)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","Panama","Gulf War")

# Table A26
stargazer(mil_desc92,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 

#1996
dip_desc96 <- data[data$fso==1 & data$wave==96,]
dip_desc96 <- data.frame(dip_desc96$age_decade,dip_desc96$gender,dip_desc96$education,dip_desc96$party_id,dip_desc96$domino_theory_bin,dip_desc96$early_bosnia,dip_desc96$force_russia1,dip_desc96$force_iraq,dip_desc96$force_cuba,dip_desc96$force_israel,dip_desc96$force_korea,dip_desc96$force_russia2,dip_desc96$force_taiwan)
covs_dip <- c("Age","Gender","Education","Party ID","Domino Theory","Bosnia","Russia-Europe","Iraq","Cuba","Israel","Korea","Russia-Poland","Taiwan")

# Table A27
stargazer(dip_desc96,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_dip)

mil_desc96 <- data[data$mil_officer==1 & data$wave==96,]
mil_desc96 <- data.frame(mil_desc96$age_decade,mil_desc96$gender,mil_desc96$education,mil_desc96$party_id,mil_desc96$domino_theory_bin,mil_desc96$early_bosnia,mil_desc96$force_russia1,mil_desc96$force_iraq,mil_desc96$force_cuba,mil_desc96$force_israel,mil_desc96$force_korea,mil_desc96$force_russia2,mil_desc96$force_taiwan)
covs_mil <- c("Age","Gender","Education","Party ID","Domino Theory","Bosnia","Russia-Europe","Iraq","Cuba","Israel","Korea","Russia-Poland","Taiwan")

# Table A28
stargazer(mil_desc96,
          type = "text",
          omit.summary.stat = c("p25", "p75"),
          style = "ajps",
          covariate.labels = covs_mil) 


###### Support for Use of Force, FPLP 1976-1992 (Table 5) ######

library(here)
library(pscl)
library(sandwich)
library(lmtest)
library(margins)

m0 <- glm(use_force ~ fso + domino_theory_bin + angola_dum + iran_dum + elsalvador_dum + lebanon_dum + grenada_dum + libya_dum + panama_dum + gulfwar_dum + bosnia_dum + russia1_dum + iraq_dum + cuba_dum + israel_dum + korea_dum + russia2_dum + taiwan_dum, data = data, family = binomial)
a <- coeftest(m0, vcov = vcovCL(m0, cluster = data$wave))
a

m1 <- glm(use_force ~ fso*domino_theory_bin + angola_dum + iran_dum + elsalvador_dum + lebanon_dum + grenada_dum + libya_dum + panama_dum + gulfwar_dum + bosnia_dum + russia1_dum + iraq_dum + cuba_dum + israel_dum + korea_dum + russia2_dum + taiwan_dum, data = data, family = binomial)
b <- coeftest(m1, vcov = vcovCL(m1, cluster = data$wave))
b

m2 <- glm(use_force ~ fso*domino_theory_bin + party_id + age + education + gender + angola_dum + iran_dum + elsalvador_dum + lebanon_dum + grenada_dum + libya_dum + panama_dum + gulfwar_dum + bosnia_dum + russia1_dum + iraq_dum + cuba_dum + israel_dum + korea_dum + russia2_dum + taiwan_dum, data = data, family = binomial)
summary(m2)
c <- coeftest(m2, vcov = vcovCL(m2, cluster = data$wave))
c

crse <- function(x, df){
  d.cse <- na.omit(df[, c("wave", all.vars(formula(x)))])
  fc <- d.cse$wave
  m <- length(unique(fc))
  k <- length(coef(x))
  u <- estfun(x)
  u.clust <- matrix(NA, nrow=m, ncol=k) 
  for(j in 1:k){
    u.clust[,j] <- tapply(u[,j], fc, sum)
  }
  bread <-vcov(x)
  c.vcov <- bread %*% ((m / (m-1)) * t(u.clust) %*% (u.clust)) %*% bread
  result <<- coeftest(x, c.vcov)
  return(result)
}

cluster.m0 <- crse(m0, data)
cluster.m1 <- crse(m1, data)
cluster.m2 <- crse(m2, data)

# Table 5
library(stargazer)
stargazer(m0, m1, m2, 
          type = "text",
          se = list(cluster.m0[,"Std. Error"], cluster.m1[,"Std. Error"], cluster.m2[,"Std. Error"]),
          label = c("tbl: fplp_overall"),
          dep.var.labels = c("Support for Use of Force"),
          covariate.labels = c("Diplomat","Domino Theory","Party ID","Age","Education","Gender","Diplomat*Domino Theory"),
          omit = c("angola_dum","iran_dum","elsalvador_dum","lebanon_dum","grenada_dum","libya_dum","panama_dum","gulfwar_dum","bosnia_dum","russia1_dum","iraq_dum","cuba_dum","israel_dum","korea_dum","russia2_dum","taiwan_dum"),
          omit.stat = c("f","ser"))

library(emmeans)
library(insight)
library(ggeffects)
library(dplyr)
ggpredict(m2, c("domino_theory_bin","fso"), ci.lvl = .9) 

####### Support for Use of Force By Intervention (Tables A31-A32) ######

# Angola
m00 <- lm(angola_involvement ~ fso*domino_theory + party_id + age_categories + education + gender, data = data76_dipmil)

# Iran
m0 <- lm(early_iran ~ fso*domino_theory + party_id + age_decade + education + gender, data = data80_dipmil)

# El Salvador
data8488_dipmil <- smartbind(data84_dipmil,data88_dipmil)
m1 <- lm(advisers_elsalvador ~ fso*domino_theory + party_id + age_decade + education + gender + wave, data = data8488_dipmil)

# Lebanon
m3 <- lm(early_lebanon ~ fso*domino_theory + party_id + age_decade + education + gender, data = data84_dipmil)

# Grenada
m4 <- lm(troops_grenada ~ fso*domino_theory + party_id + age_decade + education + gender, data = data84_dipmil)

# Libya
m5 <- lm(bomb_libya ~ fso*domino_theory + party_id + age_decade + education + gender, data = data88_dipmil)

# Panama
m6 <- lm(invade_panama ~ fso*domino_theory + party_id + age_decade + education + gender, data = data92_dipmil)

# Gulf War
m7 <- lm(early_gulfwar ~ fso*domino_theory + party_id + age_decade + education + gender, data = data92_dipmil)

# Bosnia
m9 <- lm(early_bosnia ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Russia-Europe
m11 <- lm(force_russia1 ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Iraq
m12 <- lm(force_iraq ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Cuba
m13 <- lm(force_cuba ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Israel
m14 <- lm(force_israel ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Korea
m15 <- lm(force_korea ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Russia-Poland
m16 <- lm(force_russia2 ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

# Taiwan
m18 <- lm(force_taiwan ~ fso*domino_theory + party_id + age_decade + education + gender, data = data96_dipmil)

stargazer(m00,m0,m1,m3,m4,m5,m6,m7,
          type = "text",
          dep.var.labels = c("Angola","Iran","El Salvador","Lebanon","Grenada","Libya","Panama","Gulf War"),
          covariate.labels = c("Diplomat","Domino Theory","Party ID","Age (Cohort)","Age (Decade)","Education","Gender","Wave","Diplomat*Domino Theory"),
          omit.stat = c("adj.rsq","f","ser"))

stargazer(m9,m11,m12,m13,m14,m15,m16,m18,
          type = "text",
          dep.var.labels = c("Bosnia","Russia-Europe","Iraq","Cuba","Israel","Korea","Russia-Poland","Taiwan"),
          covariate.labels = c("Diplomat","Domino Theory","Party ID","Age (Decade)","Education","Gender","Wave","Diplomat*Domino Theory"),
          omit.stat = c("adj.rsq","f","ser"))


